package com.zhixinhuixue.zsyte.xxx.app.util

import android.text.TextPaint
import android.text.style.SuperscriptSpan

/**
 * FileName:TopAlignSpan
 * Author：Administrator
 * Date:2021 2021/4/26 15:27
 * Copyright (C), 2020-2021,成都拇音科技有限公司
 *
 *
 *
 *
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
</desc></version></time></author> */


  class  BottonAlignSpan  @JvmOverloads  constructor(
    fontSizePx: Float,
    shiftPercentage: Float = 0f
) : SuperscriptSpan() {
    /**
     * 字体大小
     */
    private val fontSizePx: Float

    //shift value, 0 to 1.0
    private var shiftPercentage = 0f
    override fun updateDrawState(tp: TextPaint) {
        //original ascent
        val ascent = tp.ascent()

        //scale down the font
        tp.textSize = fontSizePx * tp.density

        //get the new font ascent
        val newAscent = tp.fontMetrics.ascent

        //move baseline to top of old font, then move down size of new font
        //adjust for errors with shift percentage
        //   tp.baselineShift += (ascent - ascent * shiftPercentage)
        //            - (newAscent - newAscent * shiftPercentage);
        //计算基线偏移量
        tp.baselineShift += ((ascent - newAscent+20f) * (1 - shiftPercentage).toInt()).toInt()
    }

    override fun updateMeasureState(tp: TextPaint) {
        updateDrawState(tp)
    }

    //sets the shift percentage
    //doesn't shift
    init {
        if (shiftPercentage > 0.0 && shiftPercentage < 1.0) {
            this.shiftPercentage = shiftPercentage
        }
        this.fontSizePx = fontSizePx
    }
}